﻿using System;
using Myotragus.Data.Emit;
using Myotragus.Data.Containers;
using Myotragus.Data.Domain;
using Myotragus.Data.Metadata;

namespace Myotragus.Data.Hbm
{
	public class DefaultsLoadEntityHbmGenerator : LocalizableEntityHbmGenerator,
		IDefaultsLoadEntityHbmGenerator
	{
		public DefaultsLoadEntityHbmGenerator(IEntitiesContainer entities, 
			ILocalizableEntityMapping mapping)
			: base(entities, mapping)
		{
		
		}

		protected const string StrLocalizableFieldFormat = @"
	<many-to-one name=""{0}"" property-ref=""TextId""
		class=""Myotragus.Data.Domain.LocalizableTextValue, Myotragus.Data""/>
	<many-to-one name=""{1}"" column=""{0}""
		class=""Myotragus.Data.Domain.DefaultsLoadLocalizableText, Myotragus.Data""/>
";

		protected override string GenerateLocalizableField(IPropertyMapping p)
		{
			return string.Format(StrLocalizableFieldFormat, p.PropertyName, 
				EmitHelper.CreateDefaultsName(p.PropertyName));
		}

		protected override Type ResolveRersistentType(Type interfaceType)
		{
			return Entities.ResolveType(
				typeof(IDefaultsLoadEntity<>).MakeGenericType(interfaceType));
		}
	}
}